//
//  HSMessageCell.swift
//  Home_School_08
//
//  Created by 张晓林 on 2016/12/29.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit

class HSMessageCell: UITableViewCell {
    // 点赞按钮回调闭包属性
    var likeBtnBlock:((_ cell:HSMessageCell) -> ())?
    // 回复按钮回调闭包属性
    var replayBtnBlock:((_ cell:HSMessageCell) -> ())?
    
    @IBOutlet weak var messageIconV: UIImageView!
    @IBOutlet weak var messageNameL: UILabel!
    @IBOutlet weak var messageUserStayleL: UILabel!
    @IBOutlet weak var messageDateL: UILabel!
    @IBOutlet weak var messageTitleL: UILabel!
    @IBOutlet weak var messageContentL: UILabel!
    @IBOutlet weak var messageLikeBtn: UIButton!
    @IBOutlet weak var messageReplayView: HSReplayListView!
    // 回复视图的高度约束
    @IBOutlet weak var messageReplayViewConst: NSLayoutConstraint!
    
    // 接受消息模型的数据
    var messageVM: HSMessageViewModel? {
        didSet {
            // 准备接口地址
            if let path = messageVM?.message.not_tpath, path != "" {
                let urlStr = "http://120.24.57.38/new/zao/" + path
                // 下载图片，使用AFN
                messageIconV.setImageWith(URL(string:urlStr)!, placeholderImage: UIImage(named: "girl"))
                
            }else {
                messageIconV.image = UIImage(named: "girl")
            }
            
            messageNameL.text = messageVM?.message.not_Name
            messageDateL.text = messageVM?.message.not_createDate
            messageTitleL.text = messageVM?.message.not_title
            messageContentL.text = messageVM?.message.not_content
            // 设置点赞数和显示高亮状态
            if (messageVM?.message.likenum)! > 0 {
                messageLikeBtn.isSelected = true
                messageLikeBtn.setTitle("\(messageVM?.message.likenum ?? 0)", for: .selected)
            }else {
                messageLikeBtn.isSelected = false
            }
            
            // 传递数据给回复列表视图
            messageReplayView.messageVM = messageVM
            // 跟新视图模型设置高度
            messageReplayViewConst.constant = messageVM?.replayListViewH ?? 0
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // 设置视图
        messageIconV.layer.cornerRadius = 25
        messageIconV.layer.masksToBounds = true
        
        messageUserStayleL.layer.cornerRadius = 8
        messageUserStayleL.layer.borderWidth = 1
        messageUserStayleL.layer.borderColor = UIColor.colorWithHex(hexColor: 0x00a0e9).cgColor
    }

    //MARK: - 点赞和回复事件方法
    @IBAction func likeBtnClicked(_ sender: UIButton) {
        likeBtnBlock?(self)
    }
   
    @IBAction func replayBtnClicked(_ sender: UIButton) {
        replayBtnBlock?(self)
    }
    
    
}
